/*
 * Copyright (c) 2011 Sicong
 *
 * All rights reserved.
 */
package com.sicong.myproject.client;

import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;


/**
 * @author sicongzhao
 *
 */
public class Info extends Content
{
    public Info()
    {
        Widget slider = getSlider();
        
        VerticalPanel wrapper = new VerticalPanel();
        wrapper.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
        
        wrapper.add(slider);
        wrapper.setStyleName("wrapper");
        
        initWidget(wrapper);
    }

    private Widget getSlider()
    {
        AbsolutePanel slider = new AbsolutePanel();
        slider.getElement().setId("slider");
        
        Label bar = new Label();
        bar.getElement().setId("bar");
        
        Image mark1 = getMark("138亿年前", "宇宙，大爆炸", false);
        Image mark2 = getMark("45亿 年前", "地球，形成", true);
        Image mark3 = getMark("40亿 年前", "生命，出现", true);
        Image mark4 = getMark("200,000 年前", "人类，起源", false);
        Image mark5 = getMark("1985", "杭州，出生", false);
        Image mark6 = getMark("2004", "珠海，大学", true);
        Image mark7 = getMark("2008", "爱丁堡，大学", false);
        Image mark8 = getMark("2010", "伦敦，工作", true);
        Image mark9 = getMark("2012", "世界，末日", true);
        
        slider.add(bar, 0, 33);
        slider.add(mark1, 10, 0);
        slider.add(mark2, 100, 45);
        slider.add(mark3, 130, 45);
        slider.add(mark4, 200, 0);
        slider.add(mark5, 300, 0);
        slider.add(mark6, 500, 45);
        slider.add(mark7, 580, 0);
        slider.add(mark8, 660, 45);
        slider.add(mark9, 740, 45);
        
        return slider;
    }
    
    private Image getMark(String time, String event, boolean flip)
    {
        Image mark;
        
        if (flip)
        {
            mark = new Image("static/resources/mark-flip.png");
        }
        else
        {
            mark = new Image("static/resources/mark.png");
        }
        
        addPop(mark, time, event, flip);
        
        return mark;
    }
    
    private void addPop(final Image mark, final String time, final String event,
                        final boolean flip)
    {
        final PopupPanel infoPop = new PopupPanel(true);
        
        mark.addMouseOverHandler(new MouseOverHandler()
        {
            @Override
            public void onMouseOver(MouseOverEvent e)
            {
                final VerticalPanel infoPanel = getInfoPanel(time, event);
                
                if (infoPop.getWidget() == null)
                {
                    infoPop.add(infoPanel);
                    infoPop.setPopupPositionAndShow(new PopupPanel.PositionCallback()
                    {
                        @Override
                        public void setPosition(int offsetWidth, int offsetHeight)
                        {
                            if (flip)
                            {
                                infoPop.setPopupPosition(mark.getAbsoluteLeft(),
                                                         mark.getAbsoluteTop() + 
                                                         mark.getOffsetHeight());
                            }
                            else
                            {
                                infoPop.setPopupPosition(mark.getAbsoluteLeft(),
                                                         mark.getAbsoluteTop() - 
                                                         infoPanel.getOffsetHeight() - 5);
                            }
                        }
                    });
                }
                else
                {
                    infoPop.show();
                }
            }
        });
        
        mark.addMouseOutHandler(new MouseOutHandler()
        {
            @Override
            public void onMouseOut(MouseOutEvent event)
            {
                infoPop.hide();
            }
        });
    }
    
    private VerticalPanel getInfoPanel(String time, String event)
    {
        Label timeLabel = new Label(time);
        Label eventLabel = new Label(event);
        
        VerticalPanel panel = new VerticalPanel();
        panel.setStyleName("info");
        
        panel.add(timeLabel);
        panel.add(eventLabel);
        
        return panel;
    }
}